package edu.wlu.cs.levy.kdTree.demo;

//Illustrates n-nearest-neighbors method of KDTree class.  Creates a KDTree
//of M keys, and finds N neighbors of D-dimensional point in center of space
//(all coords = 0.5), with N, D, M specified on command line.

import java.util.Arrays;

import edu.wlu.cs.levy.kdTree.KDTree;

public class KDNbrs {

	public static void main(String [] args) {
		args = new String[]{"1000","10","10"};

		java.util.Random r = new java.util.Random(0);

		if (args.length < 3) {
			System.err.print("Usage: java kdtime <# points> <# dims>");
			System.err.println("<# nbrs>");
			System.exit(1);
		}

		int m = Integer.parseInt(args[0]);
		int d = Integer.parseInt(args[1]);
		int n = Integer.parseInt(args[2]);

		double [][] keys = new double [m][d];

		double [] targ = new double [d];
		for (int k=0; k<d; ++k) {
			targ[k] = 0.5;
		}

		// make a D-dimensional KD-tree
		KDTree kd = new KDTree(d);

		try {

			// add M randomly keyed nodes

			for (int i=0; i<m; ++i) {
				for (int j=0; j<d; ++j) {
					keys[i][j] = r.nextDouble();
				}
				kd.insert(keys[i], new Integer(i));
			}

			// get N nearest neighbors and show their keys

			long start = time();
			Object [] nbrs = kd.nearest(targ, n);

			System.err.println((time() - start) + " msec");

			//System.exit(0);

			for (int i=0; i<nbrs.length; ++i) {
				int j = ((Integer)nbrs[i]).intValue();
				System.out.println(Arrays.toString(keys[j]));
			}
		}
		catch (Exception e) {
			System.err.println(e);
		}
	}	

	private static long time() {
		java.util.GregorianCalendar cal = new java.util.GregorianCalendar();
		return cal.getTimeInMillis();
	}



}
